<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: LanguageEditStrings.php 458 2011-01-08 07:59:22Z steen.rabol $
 *
 */

Prado::using('Application.Common.Data.PortalLanguageRecord');
Prado::using('Application.Common.Data.PortalLanguageStringRecord');

class LanguageEditStrings extends PortalAdminPage
{
	private $_langid;
	private	$_langRecord;
	private $_filter	= null;


	public function getDataItemCount($filter = null)
	{
		if($filter !== null)
		{
			$sql = "select count(*) from tbllangstrings where culture='" . $this->_langRecord->culture . "' and ($filter)";
		}
		else
		{
			$sql = "select count(*) from tbllangstrings where culture='" . $this->_langRecord->culture . "'";
		}

		return $this->DataAccess->createCommand($sql)->QueryScalar();
	}


	public function dataBind($filter = null)
	{
		$offset	= $this->LanguageStringList->CurrentPageIndex * $this->LanguageStringList->PageSize;
        $limit	= $this->LanguageStringList->PageSize;

		if($filter !== null)
		{
			$sql = "select * from tbllangstrings where culture='" . $this->_langRecord->culture . "' and ($filter) order by fromvalue limit $offset,$limit";
		}
		else
		{
			$sql = "select * from tbllangstrings where culture='" . $this->_langRecord->culture . "' order by fromvalue limit $offset,$limit";
		}

		try
		{
			$dr	= $this->DataAccess->createCommand($sql)->query();

			$this->LanguageStringList->VirtualItemCount	= $this->getDataItemCount($filter);
			$this->LanguageStringList->DataSource	= $dr;
			$this->LanguageStringList->dataBind();
		}
		catch(Exception $e)
		{
			$this->reportError(1,$e->getMessage());
		}
	}

	public function pageChanged($sender,$param)
    {
        $this->LanguageStringList->CurrentPageIndex=$param->NewPageIndex;
        $this->dataBind($this->_filter);
    }

	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->_langid		= TPropertyValue::ensureInteger($this->Request['langid']);
		$this->_langRecord	= PortalLanguageRecord::finder()->findByid($this->_langid);
		$this->setAdminPageTitle("Edit language strings");

		if(isset($this->Request['filter']) && !empty($this->Request['filter']))
		{
			$f = $this->Request['filter'];
			$sv = $this->Application->DbConnection->quoteString($f);
			$this->_filter	= "fromvalue like $sv or tovalue like $sv";
		}

		if(!$this->IsPostBack)
		{
			$this->dataBind($this->_filter);
			$this->ttbSearchValue->Text	=  $this->Request['filter'];
		}
	}

	public function AddString($sender, $param)
	{
		if($this->IsValid)
		{
			// Get Active languages
			$actLang	= PortalLanguageRecord::finder()->findAll("active =?",1);

			if($actLang !== 0 && is_array($actLang) && count($actLang) > 0)
			{
				foreach($actLang as $aLang)
				{
					$lsRec				= new PortalLanguageStringRecord();
					$lsRec->culture		= $aLang->culture;
					$lsRec->lkey		= 'APPLICATION';
					$lsRec->fromvalue	= $this->strFromValue->Text;

					if($aLang->culture	=== $this->_langRecord->culture)
					{
						$lsRec->tovalue		= $this->strToValue->Text;
					}
					else
					{
						$lsRec->tovalue		= $this->strFromValue->Text;
					}
					$lsRec->save();
				}
			}

			$this->Application->DeleteLanguageCache();
			$this->gotoPage('Admin.LanguageEditStrings',array("langid" => $this->_langid,"filter" => $this->ttbSearchValue->Text));
		}
	}

	public function SearchString($sender, $param)
	{
		if($this->IsValid)
		{
			$this->gotoPage('Admin.LanguageEditStrings',array("langid" => $this->_langid,"filter" => $this->ttbSearchValue->Text));
		}
	}

	public function ClearSearchString($sender, $param)
	{
		$this->gotoPage('Admin.LanguageEditStrings',array("langid" => $this->_langid));
	}

	public function deleteItem($sender, $param)
	{
		$id = $this->LanguageStringList->DataKeys[$param->Item->ItemIndex];
		PortalLanguageStringRecord::finder()->DeleteByid($id);
		$this->Application->DeleteLanguageCache();
		$this->gotoPage('Admin.LanguageEditStrings',array("langid" => $this->_langid,"filter" => $this->ttbSearchValue->Text));
	}

	public function itemCommand($s,$p)
	{
		switch($p->CommandName)
		{
		}
		$this->dataBind($this->_filter);
	}

	public function editItem($sender,$param)
	{
		$this->LanguageStringList->SelectedItemIndex=-1;
		$this->LanguageStringList->EditItemIndex=$param->Item->ItemIndex;
		$this->dataBind($this->_filter);
		$this->Application->DeleteLanguageCache();
	}

	public function cancelItem($sender,$param)
	{
		$this->LanguageStringList->SelectedItemIndex=-1;
		$this->LanguageStringList->EditItemIndex=-1;
		$this->dataBind($this->_filter);
	}

	public function updateItem($sender,$param)
	{
		$item=$param->Item;
		$id = $this->LanguageStringList->DataKeys[$item->ItemIndex];
		$lsRec	= PortalLanguageStringRecord::finder()->findByid($id);
		if($lsRec)
		{
			$lsRec->tovalue		= $item->edtToValue->Text;
			$lsRec->save();
		}
		$this->Application->DeleteLanguageCache();
		$this->LanguageStringList->EditItemIndex=-1;
		$this->dataBind($this->_filter);
	}

	public function getCurrentCulture()
	{
		$lr = PortalLanguageRecord::finder()->findByPk($this->Request['langid']);
		return $lr->culture;
	}
}

?>